package com.example.lxc415billiards.controller;

import com.example.lxc415billiards.domain.BilliardTable;
import com.example.lxc415billiards.service.BilliardTableService;
import com.example.lxc415billiards.untils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/billiardTable")
@Api(tags = "台球桌管理-李效丞0415")
public class BilliardTableController {

    @Autowired
    private BilliardTableService billiardTableService;

    @ApiOperation("获取所有台球桌信息")
    @GetMapping("/getAllTables")
    public AjaxResult getAllTables() {
        List<BilliardTable> tables = billiardTableService.getAllTables();
        log.info(AjaxResult.success(tables).toString());
        return AjaxResult.success(tables);
    }

    @ApiOperation("根据ID获取台球桌信息")
    @GetMapping("/getTableById/{id}")
    public AjaxResult getTableById(@PathVariable int id) {
        BilliardTable table = billiardTableService.getTableById(id);
        log.info(AjaxResult.success(table).toString());
        return table != null ? AjaxResult.success(table) : AjaxResult.error("台球桌不存在");
    }

    @ApiOperation("新增台球桌")
    @PostMapping("/addTable")
    public AjaxResult addTable(@RequestBody BilliardTable table) {
        BilliardTable newTable = billiardTableService.addTable(table);
        log.info(AjaxResult.success(newTable).toString());
        return AjaxResult.success(newTable);
    }

    @ApiOperation("根据ID删除台球桌")
    @DeleteMapping("/deleteTableById/{id}")
    public AjaxResult deleteTableById(@PathVariable int id) {
        billiardTableService.deleteTableById(id);
        log.info("删除成功");
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("修改台球桌信息")
    @PutMapping("/updateTable")
    public AjaxResult updateTable(@RequestBody BilliardTable table) {
        BilliardTable updatedTable = billiardTableService.updateTable(table);
        log.info(AjaxResult.success(updatedTable).toString());
        return updatedTable != null ? AjaxResult.success(updatedTable) : AjaxResult.error("更新失败");
    }
}
